package com.normaltest.che300escjl.utils;

import com.normaltest.che300escjl.dto.Car;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TabsExcelUtil {

    //处理excel
    public static String filePath = "D:\\java_dependency\\Project\\TEST\\che300Test\\src\\main\\resources\\车型名称映射.xlsx";
    public static List<Car> inExcel(String biao) {
        ArrayList<Car> cars = new ArrayList<>(2300);

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 获取名为 "bsheet" 的工作表
            Sheet sheet = workbook.getSheet(biao);

            if (sheet == null) {
                System.out.println("Sheet "+biao+"not found in the workbook.");
                return null;
            }

            // 遍历每一行，从第二行开始
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    // 获取第一个单元格
                    Cell firstCell = row.getCell(0);
                    String stringCellValue = firstCell.getStringCellValue();
                    Car car = fenci(stringCellValue);
                    cars.add(car);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return cars;
    }
    public static void outExcel(InputStream inputStream) {

    }
    //处理数据
    public static Car fenci(String originalString) {

        // 找到第一个空格的位置
        int index = originalString.indexOf(' ');
        Car car = new Car();
        // 如果找到了空格
        if (index != -1) {
            // 第一部分，从开头到第一个空格前
            String firstPart = originalString.substring(0, index);
            car.setZiduan1(firstPart);
            // 第二部分，从第一个空格后的字符开始到字符串末尾
            String secondPart = originalString.substring(index + 1);
            car.setZiduan2(secondPart);
        } else {
            // 没有找到空格，处理这种情况
            System.out.println("No space found in the string.");
        }
        return car;
    }

    public static void bind(HashMap<Car, Car> carCarHashMap) {
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 创建一个新的工作表
            Sheet sheet = workbook.createSheet("New Car Mapping");

            // 创建表头
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("Key Car");
            headerRow.createCell(1).setCellValue("Value Car");

            // 填充数据
            int rowNum = 1;
            for (Map.Entry<Car, Car> entry : carCarHashMap.entrySet()) {
                Row row = sheet.createRow(rowNum++);
                createCell(row, 0, entry.getKey());
                createCell(row, 1, entry.getValue());
            }

            // 自动调整列宽
            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);

            // 写入文件
            try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
                workbook.write(fileOut);
                System.out.println("Excel file updated successfully at: " + filePath);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void createCell(Row row, int columnIndex, Car car) {
        Cell cell = row.createCell(columnIndex);
        if (car != null) {
            cell.setCellValue(car.getZiduan1() + " - " + car.getZiduan2());
        } else {
            cell.setCellValue("");
        }
    }

}
